package com.ls.fw.data.exp;

import java.util.Collection;

/**
 * AND 子条件 
 * @author ls
 * 2014年12月16日下午3:28:39
 */
public class And extends Compound {
    /**
     * Internal constructor.
     */
    public And() {
        super();
    }

    /**
     * Constructs an and expression with provided expression.
     *
     * @param expressions the expression
     */
    public And(Expression... expressions) {
        super(expressions);
    }

    /**
     * Constructs an and expression with provided expression.
     *
     * @param expressions the expression
     */
    public And(Collection<Expression> expressions) {
        super(expressions);
    }

    /**
     * Adds the expression to the set of expression.
     *
     * @param expression the expression
     * @return this
     * @see Compound#add(Expression)
     */
    public And and(Expression expression) {
        return (And) super.add(expression);
    }

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		int index = 0;
		for (Expression expression : children) {
			if(index > 0){
				sb.append(" AND ");
			}
			if(expression instanceof And || expression instanceof Or){
				sb.append("("+expression+")");
			}else{
				sb.append(expression);
			}
			index++;
		}
		return sb.toString();
	}
}
